package com.sq.api.openfeignFallbackFactory;

import com.sq.api.dto.SysUserDto;
import com.sq.api.user.UserServiceApi;
import com.sq.api.user.model.LoginUser;
import com.sq.common.constants.Constants;
import com.sq.common.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @Author Li sir
 * @Date 2024/9/29
 */
@Slf4j
@Component
public class UserServiceApiFallbackFactory implements FallbackFactory<UserServiceApi> {
    @Override
    public UserServiceApi create(Throwable cause) {
        log.error("用户服务调用失败:{}", cause.getMessage());

        return new UserServiceApi() {
            @Override
            public R<LoginUser> getUserInfo(String userName) {
                return R.fail("获取用户失败:" + cause.getMessage());
            }

            @Override
            public R<LoginUser> existUser(String userName) {
                return R.fail(Constants.FAIL,userName+"用户存在:" + cause.getMessage(),null);
            }

            @Override
            public R<Integer> registerUser(SysUserDto sysUserDto) {
                return R.fail(Constants.FAIL,"用户新增失败:" + cause.getMessage(),0);
            }
        };

    }
}
